﻿using DotNetOpenAuth.OAuth2;
using System;

namespace Inovout.Web.Security.OAuth2
{
    public class OAuth2ClientDescription : ClientDescription
    {
        public OAuth2ClientDescription(string secret,string callback)
            : base(secret, null, ClientType.Confidential)
        {
            Callback = callback;
            ClientSecret = secret;
        }
        public string ClientSecret { get; private set; }
        public string Callback { get; private set; }
        public override bool IsCallbackAllowed(Uri callback)
        {
            if (string.IsNullOrEmpty(Callback))
            {
                return true;
            }
            //string[] acceptableCallbacks = this.Callback.Split(Environment.NewLine);
            
            Uri acceptableCallbackPattern = new Uri(this.Callback);
            if (string.Equals(acceptableCallbackPattern.GetLeftPart(UriPartial.Authority), callback.GetLeftPart(UriPartial.Authority), StringComparison.Ordinal))
            {
                return true;
            }
            return base.IsCallbackAllowed(callback);
        }
    }
}